<?php
namespace App\Controller\Admin;

use App\Query\ProductQuery;
use App\Repository\ProductRepository;
use App\Request\ProductRequest;

class ProductController extends Controller
{
    // 获取模型
    protected function model()
    {
        return ProductQuery::daili($this->auth()->daili_id);
    }
    
    // 列表
    public function index()
    {
        return ProductQuery::query(
            $this->model(),
            $this->inputs('name', 'menu_id')
        )->paginate(15)->toArray();
    }
    
    // 新增
    public function create(ProductRequest $request)
    {
        if(ProductRepository::make()->save($request->validated()))
        {
            $this->message('create');
        }
        
        $this->error('create');
    }
    
    // 更新
    public function update($id, ProductRequest $request)
    {
        $model = $this->model()->find($id);

        if($model && ProductRepository::make($model)->save($request->validated()))
        {
            $this->message('update');
        }
        
        $this->error('update');
    }
    
    // 删除
    public function delete($id)
    {
        $model = $this->model()->find($id);

        if($model && ProductRepository::make($model)->delete())
        {
            $this->message('delete');
        }
        
        $this->error('delete');
    }
}